package com.zycfc.zsf.boot.job.core;

import com.dangdang.ddframe.job.reg.zookeeper.*;
import com.dangdang.ddframe.job.lite.internal.storage.*;
import com.zycfc.zsf.boot.util.text.*;
import com.zycfc.zsf.boot.util.json.*;
import java.util.*;

public class ZsfJobHandler
{
    private ZookeeperRegistryCenter zookeeperRegistryCenter;
    
    public ZsfJobHandler(final ZookeeperRegistryCenter zookeeperRegistryCenter) {
        this.zookeeperRegistryCenter = zookeeperRegistryCenter;
    }
    
    public String getPartitionVal(final String jobName) {
        final JobNodePath path = new JobNodePath(jobName);
        final String val = this.zookeeperRegistryCenter.get(path.getConfigNodePath());
        if (StringUtils.isEmpty(val)) {
            return null;
        }
        final Map<String, Object> map = (Map<String, Object>)JsonUtils.jsonToMap(val);
        if (map == null || map.size() <= 0) {
            return null;
        }
        final Integer totalCount = map.get("shardingTotalCount");
        if (totalCount <= 1) {
            return "0";
        }
        final Integer result = (int)(Math.random() * totalCount);
        return result.toString();
    }
    
    public void setZookeeperRegistryCenter(final ZookeeperRegistryCenter zookeeperRegistryCenter) {
        this.zookeeperRegistryCenter = zookeeperRegistryCenter;
    }
    
    public ZookeeperRegistryCenter getZookeeperRegistryCenter() {
        return this.zookeeperRegistryCenter;
    }
}
